package com.itheima.reggie.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

/**
 * 阿里云发送短信工具类
 */
public class SmsUtil {


    /**
     * 发送短信
     *
     * @param phone 手机号
     * @param code  验证码
     */
    public static void sendSms(String phone, String code) {

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI5tKqw8zSLEadkjtmtipT",
                "oeLncgcd2MbkNjjIab0IC9nVkvNgVi");

        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("传智健康"); //签名名称
        request.setTemplateCode("SMS_195226089"); //短信模板
        request.setPhoneNumbers(phone); //手机号
        request.setTemplateParam("{\"code\":\"" + code + "\"}"); //code参数

        try {
            SendSmsResponse response = client.getAcsResponse(request); //发送短信
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }

}
